package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

  @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders)
  {
      ordersService.submit(orders);

      return R.success("订单提交成功");
  }

  @GetMapping("/userPage")
    public R<Page> get(int page, int pageSize)
  {
      Long UserId = BaseContext.getCurrentId();
      Page<Orders> pageInfo=new Page<>(page,pageSize);
      LambdaQueryWrapper<Orders> lqw=new LambdaQueryWrapper<>();
      lqw.orderByDesc(Orders::getOrderTime);
      lqw.eq(Orders::getUserId,UserId);

      ordersService.page(pageInfo,lqw);

      return R.success(pageInfo);

  }
    @GetMapping("/page")

    public R<Page> getPage(int page,int pageSize,Long number,String beginTime,String endTime)
    {
        System.out.println(beginTime);
        System.out.println(endTime);
        Page<Orders> pageInfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> lqw=new LambdaQueryWrapper<>();
        lqw.like(number!=null,Orders::getId,number);
        if(beginTime!=null||endTime!=null) {
            lqw.between(Orders::getOrderTime, beginTime, endTime);
        }

        ordersService.page(pageInfo,lqw);

        return R.success(pageInfo);

    }
    @PutMapping
    public R<String> put(@RequestBody Orders orders)
    {
        System.out.println(orders);
        LambdaQueryWrapper<Orders> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Orders::getId,orders.getId());

        Orders ordersServiceOne = ordersService.getOne(lqw);
        ordersServiceOne.setStatus(orders.getStatus());
        ordersService.updateById(ordersServiceOne);

        return R.success("修改成功");
    }
    @PostMapping("/again")
    public R<Orders> again(@RequestBody Orders orders)

    {

        return R.success(orders);
    }


}
